const express = require('express')
const origin_index = require('./json/1-商务会议协议_index')
const origin_ocr = require('./json/1-商务会议协议_OCR')

const modified_index = require('./json/2-商务会议协议-扫描_index')
const modified_ocr = require('./json/2-商务会议协议-扫描_OCR')

const formatData = require('./format')

const app = express()
const port = 3000

app.use(express.static('static'))

app.all("*",function(req,res,next){
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin","*");
    //允许的header类型
    res.header("Access-Control-Allow-Headers","content-type");
    //跨域允许的请求方式
    res.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS");
    if (req.method.toLowerCase() == 'options')
        res.send(200);  //让options尝试请求快速结束
    else
        next();
});

app.get('/detail', (req, res) => {
  res.json({
      code: 200,
      data: {
        origin: {
            index: origin_index,
            ocr: origin_ocr,
        },
        modified: {
            index: modified_index,
            ocr: modified_ocr,
        }
      }
  })
})

app.get('/task/detail', (req, res) => {
  res.json({
    code: 200,
    data: {
      origin_name: '1-商务会议协议',
      modified_name: '2-商务会议协议-扫描',
      comparePages: [...formatData({
        origin: {
          index: origin_index,
          ocr: origin_ocr,
        },
        modified: {
            index: modified_index,
            ocr: modified_ocr,
        }
      }, req.query.debug)]
    }
  })
})

app.get('/task/detail2', (req, res) => {
  res.json({
    code: 200,
    data: {
      origin_name: '1-商务会议协议',
      modified_name: '2-商务会议协议-扫描',
      comparePages: [
        { 
          origin: {
            img_url: 'http://localhost:3000/origin/000.png',
            img_size: { w: 4896, h: 6336 },
            masks: [
              {
                type: '-',
                content: '商',
                bbox_index: '0.0.0',
                bboxes: [
                  { x: 1936, y: 825, w: 1014, h: 166 },
                ]
              },
              {
                type: '+',
                content: '商',
                bbox_index: '0.0.0',
                bboxes: [
                  { x: 2936, y: 825, w: 1014, h: 166 },
                ]
              },
              {
                type: '^',
                content: '商',
                bbox_index: '0.0.0',
                bboxes: [
                  { x: 3936, y: 825, w: 1014, h: 166 },
                ]
              }
            ]
          }, 
          modified: {
            img_url: 'http://localhost:3000/modified/000.png',
            masks: [],
          }
        },
        // { origin: 'http://localhost:3000/origin/001.png', modified: 'http://localhost:3000/modified/001.png' },
        // { origin: 'http://localhost:3000/origin/002.png', modified: 'http://localhost:3000/modified/002.png' },
        // { origin: 'http://localhost:3000/origin/003.png', modified: 'http://localhost:3000/modified/003.png' },
        // { origin: 'http://localhost:3000/origin/004.png', modified: 'http://localhost:3000/modified/004.png' },
        // { origin: 'http://localhost:3000/origin/005.png', modified: 'http://localhost:3000/modified/005.png' },
        // { origin: 'http://localhost:3000/origin/006.png', modified: 'http://localhost:3000/modified/006.png' },
        // { origin: 'http://localhost:3000/origin/007.png', modified: 'http://localhost:3000/modified/007.png' },
        // { origin: 'http://localhost:3000/origin/008.png', modified: 'http://localhost:3000/modified/008.png' },
        // { origin: 'http://localhost:3000/origin/009.png', modified: 'http://localhost:3000/modified/009.png' },
        // { origin: 'http://localhost:3000/origin/010.png', modified: 'http://localhost:3000/modified/010.png' },
      ],
      // compareList: [
      //   { 
      //     type: 'del',
      //     origin: {
      //       content: '商',
      //       symbol: ['-'],
      //       bbox_index: '0.0.0',
      //       bboxes: [
      //         [
      //           [1936, 825],
      //           [2950, 825],
      //           [2950, 991],
      //           [1936, 991]
      //         ],
      //       ]
      //     }, 
      //     modified: { content: '' },
      //   },
      //   { 
      //     type: 'del_o',
      //     origin: {
      //       content: ':',
      //       symbol: ['o-'],
      //       bbox_index: '0.2.7',
      //       bboxes: [
      //         [
      //           [
      //               593,
      //               1221
      //           ],
      //           [
      //               2669,
      //               1221
      //           ],
      //           [
      //               2669,
      //               1376
      //           ],
      //           [
      //               593,
      //               1376
      //           ]
      //         ],
      //       ]
      //     }, 
      //     modified: { content: '' },
      //   },
      //   { 
      //     type: 'add',
      //     origin: {
      //       content: '商务',
      //       symbol: ['+', '+'],
      //     }, 
      //     modified: { content: '' },
      //   },
      //   { 
      //     type: 'update',
      //     origin: {
      //       content: '个人',
      //       symbol: ['^', '^'],
      //     }, 
      //     modified: {
      //       content: '多数',
      //       symbol: ['^', '^'],
      //     },
      //   },
      // ],
    }
  })
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})